﻿/*
 * SystemInfoSnapshot
 * Author: Tiago Conceição
 * 
 * http://systeminfosnapshot.com/
 * https://github.com/sn4k3/SystemInfoSnapshot
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace SystemInfoSnapshot.Core.Malware
{
    public class MalwareManager : IEnumerable<MalwareItem>
    {
        #region Singleton
        /// <summary>
        /// A instance of this class
        /// </summary>
        private static MalwareManager _instance;

        /// <summary>
        /// Gets the singleton instance of this class
        /// </summary>
        public static MalwareManager Instance
        {
            get { return _instance ?? (_instance = new MalwareManager()); }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the malware items
        /// </summary>
        public List<MalwareItem> Items { get; private set; }
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        private MalwareManager()
        {
            Items = new List<MalwareItem>();
            foreach (var item in Properties.Resources.malware.Split(new[] { "\r\n", "\n"}, StringSplitOptions.RemoveEmptyEntries))
            {
                Add(item, MalwareType.Toolbar);
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Add a malware to the list.
        /// </summary>
        /// <param name="item">Malware item.</param>
        public void Add(MalwareItem item)
        {
            Items.Add(item);
        }

        /// <summary>
        /// Add a malware to the list.
        /// </summary>
        /// <param name="items">Malware list.</param>
        public void AddRange(MalwareItem[] items)
        {
            Items.AddRange(items);
        }

        /// <summary>
        /// Add a malware to the list.
        /// </summary>
        /// <param name="name">Malware name.</param>
        /// <param name="type">Malware type.</param>
        /// <param name="exactMatch">True if malware must exactly match with a software name or only need to contains all words in it, otherwise false.</param>
        public void Add(string name, MalwareType type, bool exactMatch = false)
        {
            Items.Add(new MalwareItem(name, type, exactMatch));
        }

        /// <summary>
        /// Checks if a name exists in the malware database
        /// </summary>
        /// <param name="name">Name to check</param>
        /// <returns>True if exists, otehrwise false</returns>
        public bool Contains(string name)
        {
            var item = this[name];
            return !ReferenceEquals(item, null);
        }
        #endregion

        #region Overrides
        public IEnumerator<MalwareItem> GetEnumerator()
        {
            return Items.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        #endregion

        #region Indexers
        /// <summary>
        /// Indexers 
        /// </summary>
        /// <param name="index">index</param>
        /// <returns></returns>
        public MalwareItem this[int index]    // Indexer declaration
        {
            get
            {
                return Items[index];
            }

            set
            {
                Items[index] = value;
            }
        }

        /// <summary>
        /// Indexers 
        /// </summary>
        /// <param name="name">name</param>
        /// <returns></returns>
        public MalwareItem this[string name]    // Indexer declaration
        {
            get
            { return Items.FirstOrDefault(item => item.Equals(name)); }
        }
        #endregion
    }
}
